package com.innovative.androiddemo1;

import com.innovative.androiddemo1.cls.Addresses;
import com.innovative.androiddemo1.cls.Item_Rest;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBAdapter {
	Context context;
	private DatabaseHelper DBHelper;
	private SQLiteDatabase db;
	String TABLE1 = "userinfo";

	public DBAdapter(Context cn) {
		this.context = cn;
		DBHelper = new DatabaseHelper(context);
	}

	private static class DatabaseHelper extends SQLiteOpenHelper {
		DatabaseHelper(Context context) {
			super(context, "database.db", null, 1);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			// TODO Auto-generated method stub
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub
		}

	}

	// ---opens the database---
	public DBAdapter open() throws SQLException {
		db = DBHelper.getWritableDatabase();
		return this;
	}

	// ---closes the database---
	public void close() {
		DBHelper.close();
		db.close();
	}

	public Cursor getDetails() throws SQLException {
		Cursor mCursor = db.rawQuery("select * FROM userinfo", null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}

	public long addDrafts(String lasttime, int id, String name, String type,
			String type_id, String city, String str_name, String str_no,
			String lat, String lon) {
		// TODO Auto-generated method stub
		// Log.e("iFax.rec_id : Database File ",""+iFax.rec_id);
		ContentValues initialValues = new ContentValues();
		initialValues.put("lasttime", lasttime);
		initialValues.put("id", id);
		initialValues.put("name", name);
		initialValues.put("type", type);
		initialValues.put("type_id", type_id);
		initialValues.put("city", city);
		initialValues.put("str_name", str_name);
		initialValues.put("str_no", str_no);
		initialValues.put("lat", lat);
		initialValues.put("lon", lon);

		long temp = db.insert("Restaurants", null, initialValues);

		// db.update("Restaurants", initialValues, FAX_ID + "="
		// + iFax.rec_id, null);

		return temp;
	}

	public long addDrafts() {
		String lasttime = "";
		
		lasttime = Constants.restaurants.getLastTime();
		Item_Rest it = Constants.restaurants.getItems().get(0);
		Addresses ad = it.getAddress();
		ContentValues initialValues = new ContentValues();
		initialValues.put("lasttime", lasttime);
//		initialValues.put("id", it.getID());
		initialValues.put("name", it.getName());
		initialValues.put("type", it.getType());
		initialValues.put("type_id", it.getTypeID());
		initialValues.put("city", ad.getCity());
		initialValues.put("str_name", ad.getStreetName());
		initialValues.put("str_no", ad.getStreetNumber());
		initialValues.put("lat", "afs");
		initialValues.put("lon", "asdf");
		
//		initialValues.put("lasttime", "asdf");
//		initialValues.put("id", 1);
//		initialValues.put("name", "asdf");
//		initialValues.put("type", "asdf");
//		initialValues.put("type_id", "asdf");
//		initialValues.put("city", "asdf");
//		initialValues.put("str_name","asdf");
//		initialValues.put("str_no", "asdf");
//		initialValues.put("lat", "afs");
//		initialValues.put("lon", "asdf");
		long temp = db.insert("Restaurants", null, initialValues);

		// db.update("Restaurants", initialValues, FAX_ID + "="
		// + iFax.rec_id, null);

		return temp;
	}
}
